﻿package Cain.mk
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.utils.getDefinitionByName;
	import gs.TweenMax;
	import gs.easing.*;
	/**
	 * ...
	 * @author Cain@2009 in ADCAST
	 */
	public class Lightbox extends Sprite
	{
		private var fun_time:Number;
		private var fun_wait_time:Number;
		private var pcx:DisplayObject;
		public var next_pcx:String;
		private var ca_height:Number;
		private var last_ca_height:Number;
		private var ca_num:int;
		private var pcx1_arr:Array;
		private var pcx2_arr:Array;
		private var pcx_parent:Sprite;
		private var pcx_x:Number;
		private var pcx_y:Number;
		public function Lightbox(_pcx:DisplayObject, _next_pcx:String, _ca_height:Number = 20, _fun_time:Number = 1, _fun_wait_time:Number = 0.1)
		{
			pcx = _pcx;
			pcx_parent = Sprite(pcx.parent);
			next_pcx = _next_pcx;
			ca_height = _ca_height;
			fun_time = _fun_time;
			fun_wait_time = _fun_wait_time;
			pcx1_arr = new Array();
			pcx2_arr = new Array();
			pcx_x = pcx.x;
			pcx_y = pcx.y;
			init();
		}
		
		private function init():void
		{
			ca_num = set_ca_num(pcx.height, ca_height);//計算可切幾塊;
			last_ca_height = set_last_ca_height(pcx.height, ca_height, ca_num);//計算最後一塊多大
			copy_pcx1();//pcx1切圖
			fun_pcx1();//pcx1翻牌
			copy_pcx2();//pcx2切圖
			fun_pcx2();//pcx2翻牌;
		}
		
		private function fun_pcx1():void
		{
			for (var i in pcx1_arr) 
			{
				fun(pcx1_arr[i],i); //翻
			}
		}
			private function fun_pcx2():void
		{
			for (var i in pcx1_arr) 
			{
				fun2(pcx2_arr[i],i); //翻
			}
		}
		private function fun(mc:DisplayObject,_t:int):void
		{//翻
			TweenMax.to(mc, fun_time, { scaleY:0,tint:0x000000,delay:_t*fun_wait_time, ease:Cubic.easeInOut} );
		}
		private function fun2(mc:DisplayObject,_t:int):void
		{//翻
			TweenMax.from(mc, fun_time, { scaleY:0,tint:0x000000,delay:_t*fun_wait_time, ease:Cubic.easeInOut} );
		}
		
		private function set_last_ca_height(hh:Number,ca:Number,num:int):Number
		{
			var n:Number = hh - ca * (num - 1);
			return n;
		}
		
		private function set_ca_num(hh:Number,ca:Number):int
		{//計算可切幾塊;
			var n:int;
			n = Math.floor(hh / ca);
			if (hh%n!=0) 
			{
				n++;
			}
			return n;
		}
		
		private function copy_pcx1():void
		{//pcx1切圖
			for (var i:int = 0; i < ca_num-1; i++) 
			{
				var bitda:BitmapData = new BitmapData(pcx.width, ca_height);
				var newMatrix:Matrix = new Matrix();
				newMatrix.translate( 0, i*-ca_height);
				bitda.draw(pcx,newMatrix);
				var bit:Bitmap = new Bitmap(bitda);
				var sp:Sprite = new Sprite();
				sp.addChild(bit);
				bit.y = -bit.height;
				sp.x = pcx.x;
				sp.y = pcx.y + i * ca_height+sp.height;
				Sprite(pcx.parent).addChild(sp);
				pcx1_arr.push(sp);
			}
			
			var bitda2:BitmapData = new BitmapData(pcx.width, last_ca_height);
			var newMatrix2:Matrix = new Matrix();
			newMatrix2.translate( 0, -(ca_num-1)*ca_height);
			bitda2.draw(pcx,newMatrix2);
			var bit2:Bitmap = new Bitmap(bitda2);
			var sp2:Sprite = new Sprite();
			sp2.addChild(bit2);
			bit2.y = -bit2.height;
			sp2.x = pcx.x;
			sp2.y = sp2.y + (ca_num-1) * ca_height+sp2.height;
			Sprite(pcx.parent).addChild(sp2);
			pcx1_arr.push(sp2);
	
			pcx_parent.removeChild(pcx);
		}
		private function copy_pcx2():void
		{//pcx2切圖
			var pcx2_class:Class = getDefinitionByName(next_pcx) as Class;
			var pcx2_mc = new pcx2_class();
			//pcx_parent.addChild(pcx2_mc);
			for (var i:int = 0; i < ca_num-1; i++) 
			{
				var bitda:BitmapData = new BitmapData(pcx2_mc.width, ca_height);
				var newMatrix:Matrix = new Matrix();
				newMatrix.translate( 0, i*-ca_height);
				bitda.draw(pcx2_mc,newMatrix);
				var bit:Bitmap = new Bitmap(bitda);
				var sp:Sprite = new Sprite();
				sp.addChild(bit);
				bit.y = 0;
				sp.x = pcx_x;
				sp.y = pcx_y + i * ca_height;
				pcx_parent.addChild(sp);
				pcx2_arr.push(sp);
			}
			
			var bitda2:BitmapData = new BitmapData(pcx2_mc.width, last_ca_height);
			var newMatrix2:Matrix = new Matrix();
			newMatrix2.translate( 0, -(ca_num-1)*ca_height);
			bitda2.draw(pcx2_mc,newMatrix2);
			var bit2:Bitmap = new Bitmap(bitda2);
			var sp2:Sprite = new Sprite();
			sp2.addChild(bit2);
			bit2.y = 0;
			sp2.x = pcx_x;
			sp2.y = sp2.y + (ca_num-1) * ca_height;
			pcx_parent.addChild(sp2);
			pcx2_arr.push(sp2);
	
			//pcx_parent.removeChild(pcx2_mc);
		}
	}
	
}